﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Timer
{
    public partial class frmSectionSetting : Form
    {
        private IList<SectionEntity> _sections;
        private IList<FontSizeEntity> _fonts;
        private int _sectionID;
        private XMLHelper _cc = null;


        public frmSectionSetting(IList<SectionEntity> sections, IList<FontSizeEntity> fonts, XMLHelper CC)
        {
            InitializeComponent();
            this._sectionID = 0;
            this._sections = sections;
            this._fonts = fonts;
            this._cc = CC;
        }

        /// <summary>
        /// Set section button style.
        /// </summary>
        /// <param name="section">
        /// 1:第一段
        /// 2:第二段
        /// 3:第三段
        /// 4:第四段
        /// 5:字体
        /// </param>
        private void SetSectionsStatus(int section)
        {
            this.btnSection1.FlatStyle = FlatStyle.Popup;
            this.btnSection2.FlatStyle = FlatStyle.Popup;
            this.btnSection3.FlatStyle = FlatStyle.Popup;
            this.btnSection4.FlatStyle = FlatStyle.Popup;
            this.btnFont.FlatStyle = FlatStyle.Popup;

            switch (section)
            {
                case 1:
                    this.btnSection1.FlatStyle = FlatStyle.Flat;
                    break;
                case 2:
                    this.btnSection2.FlatStyle = FlatStyle.Flat;
                    break;
                case 3:
                    this.btnSection3.FlatStyle = FlatStyle.Flat;
                    break;
                case 4:
                    this.btnSection4.FlatStyle = FlatStyle.Flat;
                    break;
                default:
                    this.btnFont.FlatStyle = FlatStyle.Flat;
                    break;
            }

            if (section < 5)
            {
                this.panSections.Visible = true;
                this.panFont.Visible = false;
                this.panSections.Location = new Point(6, 23);
            }
            else
            {
                this.panSections.Visible = false;
                this.panFont.Visible = true;
                this.panFont.Location = new Point(3, 23);
            }

        }

        /// <summary>
        /// Set value for selected section.
        /// </summary>
        /// <param name="section"></param>
        private void SetValueForSection(int section)
        {
            if (section < 5)
            {
                SectionEntity ent = this._sections[section - 1];
                this.dtpTotalTime.Text = ent.TotalTime;
                this.dtpAlertTime.Text = ent.AlertTime;
                this.txtAlertSoundFile.Text = ent.AlertSoundFile;
                this.txtFinishSoundFile.Text = ent.FinishSoundFile;
                this.txtShowTitle.Text = ent.SectionTitle;
            }
            else
            {
                this.nudTitleFontSize.Value = Convert.ToDecimal(this._fonts[0].TitleFontSize);
                this.nudTimeFontSize.Value = Convert.ToDecimal(this._fonts[0].TimeFontSize);
            }

        }

        private void IntiializeSectionForm()
        {
            // Set window size
            this.groupBox1.Size = new Size(292, 182);
            this.Size = new Size(317, 297);

            this.btnSection1.Tag = this._sections[0].ID;
            this.btnSection2.Tag = this._sections[1].ID;
            this.btnSection3.Tag = this._sections[2].ID;
            this.btnSection4.Tag = this._sections[3].ID;
            this.btnFont.Tag = 5;

            this.btnSection1.Select();
            this.btnSection1_Click(this.btnSection1, EventArgs.Empty);
        }

        private string ChooseWavFile()
        {
            string fileName = string.Empty;
            OpenFileDialog openFileDia = new OpenFileDialog();
            openFileDia.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            openFileDia.Filter = "Wav 文件|*.wav|所有文件|*.*";

            if (openFileDia.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(openFileDia.FileName);

                // 如果程序安装目录，那么将选中的wav文件复制到程序的安装目录下，如果安装目录有同名文件，则取消复制操作
                if (AppDomain.CurrentDomain.BaseDirectory != string.Format("{0}\\", fileInfo.DirectoryName))
                {
                    if (!File.Exists(string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, fileInfo.Name)))
                    {
                        try
                        {
                            fileInfo.CopyTo(string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, fileInfo.Name));
                        }
                        catch
                        {
                            fileName = string.Empty;
                        }
                    }
                }

                fileName = fileInfo.Name;
            }

            return fileName;
        }


        /// <summary>
        /// Form load.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSectionSetting_Load(object sender, EventArgs e)
        {
            this.IntiializeSectionForm();
        }

        /// <summary>
        /// Section 1 to 5 and font size.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSection1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                this._sectionID = Convert.ToInt32(btn.Tag);
                this.SetSectionsStatus(this._sectionID);
                this.SetValueForSection(this._sectionID);

                this.groupBox1.Text = btn.Text;
            }
        }

        /// <summary>
        /// Click OK button to save data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            int flag = 0;
            switch (this._sectionID)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                    flag = this._cc.Save(new SectionEntity
                    {
                        ID = this._sectionID,
                        TotalTime = this.dtpTotalTime.Value.ToString("T"),
                        AlertTime = this.dtpAlertTime.Value.ToString("T"),
                        AlertSoundFile = this.txtAlertSoundFile.Text,
                        FinishSoundFile = this.txtFinishSoundFile.Text,
                        SectionTitle = this.txtShowTitle.Text
                    });
                    break;
                default:
                    flag = this._cc.Save(new FontSizeEntity
                    {
                        TimeFontSize = Convert.ToInt32(this.nudTimeFontSize.Value),
                        TitleFontSize = Convert.ToInt32(this.nudTitleFontSize.Value)
                    });
                    break;
            }


            if (flag == 1)
            {
                MessageBox.Show("保存成功");
            }
            else if (flag == -1)
            {
                MessageBox.Show("保存失改");
            }

        }

        /// <summary>
        /// Choose .wav file for alert.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearchAlertSoundFile_Click(object sender, EventArgs e)
        {
            string fileName = this.ChooseWavFile();
            if (!string.IsNullOrEmpty(fileName))
            {
                this.txtAlertSoundFile.Text = fileName;
            }
        }

        /// <summary>
        /// Choose .wav file for finish.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearchFinishSoundFile_Click(object sender, EventArgs e)
        {
            string fileName = this.ChooseWavFile();
            if (!string.IsNullOrEmpty(fileName))
            {
                this.txtFinishSoundFile.Text = fileName;
            }
        }
    }
}
